#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
       int n=nums.size(),flag=0;
       for(int i=0;i<n;i++)
       {
        if(nums[i]<0)flag=i;
        else break;
       }
       int i=flag,j=i+1;
        vector<int>ans;
        while(i>=0&&j<n)
        {
            if(abs(nums[j])<=abs(nums[i]))ans.push_back(nums[j]*nums[j++]);
            else ans.push_back(nums[i]*nums[i--]);
        }
        while(i>=0){
            ans.push_back(nums[i]*nums[i]);
            i--;
        }
        while(j<n){
            ans.push_back(nums[j]*nums[j]);
            j++;
        }
        return ans;
        
    }
};
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n=nums.size();
        vector<int>ans(n);
        int i=0,j=n-1,cur=n-1;
        while(i<=j)
        {
            if(nums[i]*nums[i]<=nums[j]*nums[j]){
                ans[cur]=nums[j]*nums[j];
                j--;
            }
            else {
                ans[cur]=nums[i]*nums[i];
                i++;
            }
            cur--;
        }
        return ans;
    }
};